﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using PHMEE.Admin.Controllers;
using Volo.Abp.Application.Dtos;
using Volo.Abp.FeatureManagement;


namespace PHMEE.Admin.HttpApi.Controllers.DaqInfo.Thresholds
{
    /// <summary>
    /// 阈值模块
    /// </summary>
    [Route("phmeeadmin/api/[controller]/[action]")]
    public class ThresholdController : PHMEEAdminController
    {
        private readonly IThresholdAppService _thresholdAppService;

        public ThresholdController(IThresholdAppService threshAppService)
        {
            _thresholdAppService = threshAppService;
        }
        /// <summary>
        /// 获取模拟测点id
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public List<TestPointDto> GetPointIds()
        {
            return _thresholdAppService.GetPointIds();
        }
        /// <summary>
        /// 根据id查询采集定义类型
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<CategoryLookupDto> GetCategorieById(Guid guid)
        {
            return await _thresholdAppService.GetCategorieByIdAsync(guid);
        }

        /// <summary>
        /// 获取所有的采集定义类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<ListResultDto<CategoryLookupDto>> GetCategories()
        {
            return await _thresholdAppService.GetCategoriesAsync();
        }
        /// <summary>
        /// 查询所有阈值--根据测点id
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public Task<List<ThresholdDto>> GetAllThresholds(Guid guid)
        {
            return _thresholdAppService.GetAllThresholdsAsync(guid);
        }

        /// <summary>
        /// 查询所有启用阈值--根据测点id，供数采器配置时使用
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public Task<List<ThresholdDto>> GetIsTrueThresholds(Guid guid)
        {
            return _thresholdAppService.GetThresholdsIsTrueAsync(guid);
        }
        /// <summary>
        /// 创建阈值-单条
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task CreateThreshold(CreateThresholdDto input)
        {
            await _thresholdAppService.CreateAsync(input);
        }
        /// <summary>
        /// 创建阈值--支持多条
        /// </summary>
        /// <param name="inputs"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> CreateMultipleThresholds([FromBody] List<CreateThresholdDto> inputs)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            try
            {
                int count = await _thresholdAppService.CreateMultipleAsync(inputs);
                return Ok(new { Message = "Success", Count = count });
            }
            catch (Exception ex)
            {
                return StatusCode(500, "Internal server error: " + ex.Message);
            }
        }
    }
}
